const app = getApp();
const common = require('../../../utils/common.js');
const api = require('../../../config/api.js');
Page({
    /** 
     * 页面的初始数据
     */
    data: {
        todaySign: false,
        sysW: null,
        lastDay: null,
        firstDay: null,
        year: null,
        hasEmptyGrid: false,
        cur_year: '',
        cur_month: '',
        firstDay: null,
        getDate: null,
        month: null,
        display: "none",
        week: [{
            wook: "一"
        }, {
            wook: "二"
        }, {
            wook: "三"
        }, {
            wook: "四"
        }, {
            wook: "五"
        }, {
            wook: "六"
        }, {
            wook: "日"
        }, ],
        day: [{
            wook: '',
            // src: "../../image/newspaper.png",
        }, {
            wook: ''
        }, {
            wook: ''
        }, {
            wook: ''
        }, {
            wook: ''
        }, {
            wook: ''
        }, {
            wook: ''
        }],
        days: [

        ]
    },
    //签到记录
    signList: function (data) {
        common.request(api.signList, data ? {
            queryDate: data
        } : {}, 'post').then(data => {
            console.log(data)
            if (data.type == 'success') {
                this.data.dataList = data.data.dataList
                this.setData(data.data)
            }else{
                this.data.dataList = []
            }
        })
    },
    getProWeekList: function () {
        let that = this
        let date = new Date()
        let dateTime = date.getTime(); // 获取现在的时间
        let dateDay = date.getDay(); // 获取现在的
        let oneDayTime = 24 * 60 * 60 * 1000; //一天的时间
        let proWeekList;
        let weekday;
        console.log(dateTime)
        for (let i = 0; i < 7; i++) {
            let time = dateTime - (dateDay - 1 - i) * oneDayTime;
            proWeekList = new Date(time).getDate(); //date格式转换为yyyy-mm-dd格式的字符串
            weekday = "day[" + i + "].wook"
            that.setData({
                [weekday]: proWeekList,
            })
            //that.data.day[i].wook = new Date(time).getDate();
        }
    },
    dateSelectAction: function (e) {
        let cur_day = e.currentTarget.dataset.idx;
        this.setData({
            todayIndex: cur_day
        })
        console.log(`点击的日期:${this.data.cur_year}年${this.data.cur_month}月${cur_day + 1}日`);
    },

    setNowDate: function () {
        const date = new Date();
        const cur_year = date.getFullYear();
        const cur_month = date.getMonth() + 1;
        const todayIndex = date.getDate();
        console.log(`日期：${todayIndex}`)
        const weeks_ch = ['日', '一', '二', '三', '四', '五', '六'];
        this.calculateEmptyGrids(cur_year, cur_month);
        this.calculateDays(cur_year, cur_month);
        this.setData({
            cur_year: cur_year,
            cur_month: cur_month,
            weeks_ch,
            todayIndex,
        })
    },

    getThisMonthDays(year, month) {
        return new Date(year, month, 0).getDate();
    },
    getFirstDayOfWeek(year, month) {
        return new Date(Date.UTC(year, month - 1, 1)).getDay();
    },
    calculateEmptyGrids(year, month) {
        const firstDayOfWeek = this.getFirstDayOfWeek(year, month);
        let empytGrids = [];
        if (firstDayOfWeek > 0) {
            for (let i = 0; i < firstDayOfWeek; i++) {
                empytGrids.push(i);
            }
            this.setData({
                hasEmptyGrid: true,
                empytGrids
            });
        } else {
            this.setData({
                hasEmptyGrid: false,
                empytGrids: []
            });
        }
    },
    calculateDays(year, month) {
        let days = [];
        let weekday;
        let weekdays
        const thisMonthDays = this.getThisMonthDays(year, month);

        let signlist = this.data.dataList
        if (signlist.length) {
            var arrg = []
            for (let j = 0; j < signlist.length; j++) {
                arrg.push(parseInt(signlist[j].signTime.substr(signlist[0].signTime.length - 2, signlist[0].signTime.length)))

            }
            console.log(arrg)
        }

        for (let i = 1; i <= thisMonthDays; i++) {
            // days[i].push(i);
            weekday = "days[" + (i - 1) + "].item"
            console.log(i)
            if (signlist.length && arrg.indexOf(i) > -1) {
                weekdays = "days[" + (i - 1) + "].src"
            }
            console.log(weekday, "变量名")
            this.setData({
                [weekday]: i,
                [weekdays]: i,
                src: ''
            })
        }


        console.log(this.data.days)
    },
    handleCalendar(e) {
        const handle = e.currentTarget.dataset.handle;
        const cur_year = this.data.cur_year;
        const cur_month = this.data.cur_month;

        this.setData({
            days: []
        })


        if (handle === 'prev') {
            let newMonth = cur_month - 1;
            let newYear = cur_year;
            if (newMonth < 1) {
                newYear = cur_year - 1;
                newMonth = 12;
            }
            let data = newYear + '-' + newMonth + '-01'
            this.signList(data)
            setTimeout(() => {
                this.calculateDays(newYear, newMonth);
                this.calculateEmptyGrids(newYear, newMonth);

                let firstDay = new Date(newYear, newMonth - 1, 1);
                this.data.firstDay = firstDay.getDay();
                this.setData({
                    cur_year: newYear,
                    cur_month: newMonth,
                    marLet: this.data.firstDay
                })
                if (this.data.month == newMonth) {
                    this.setData({
                        judge: 1
                    })
                } else {
                    this.setData({
                        judge: 0
                    })
                }
            }, 500)



        } else {
            let newMonth = cur_month + 1;
            let newYear = cur_year;
            if (newMonth > 12) {
                newYear = cur_year + 1;
                newMonth = 1;
            }

            let data = newYear + '-' + newMonth + '-01'
            this.signList(data)

            setTimeout(() => {
                this.calculateDays(newYear, newMonth);
                this.calculateEmptyGrids(newYear, newMonth);
                let firstDay = new Date(newYear, newMonth - 1, 1);
                this.data.firstDay = firstDay.getDay();
                this.setData({
                    cur_year: newYear,
                    cur_month: newMonth,
                    marLet: this.data.firstDay
                })
                if (this.data.month == newMonth) {
                    this.setData({
                        judge: 1
                    })
                } else {
                    this.setData({
                        judge: 0
                    })
                }
            }, 500)


        }
        // console.log(this.data.cur_year, this.data.cur_month)

    },
    dataTime: function () {
        var date = new Date();
        var year = date.getFullYear();
        var month = date.getMonth();
        var months = date.getMonth() + 1;

        //获取现今年份
        this.data.year = year;

        //获取现今月份
        this.data.month = months;

        //获取今日日期
        this.data.getDate = date.getDate();

        //最后一天是几号
        var d = new Date(year, months, 0);
        this.data.lastDay = d.getDate();

        //第一天星期几
        let firstDay = new Date(year, month, 1);
        this.data.firstDay = firstDay.getDay();
    },
    onshow: function () {
        this.setData({
            display: "block",
        })
        this.signList()
    },
    onhide: function () {
        this.setData({
            display: "none",
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.data.dataList = JSON.parse(options.list);
        var that = this;
        this.setNowDate();
        this.getProWeekList()

        this.dataTime();
        var res = wx.getSystemInfoSync();
        this.setData({
            sysW: res.windowHeight / 12 - 5, //更具屏幕宽度变化自动设置宽度
            marLet: this.data.firstDay,
            getDate: this.data.getDate,
            judge: 1,
            month: this.data.month,
        });

        /**
         * 获取系统信息
         */
        console.log(that.data.month)
    },
   
   
    //签到接口
    sign: function () {
        common.request(api.sign, {}, 'post').then(data => {
            console.log(data)
            if (data.type == 'success') {
                common.showSuccessToast(data.content)
            }
            common.request(api.signList, {}, 'post').then(data => {
                if (data.type == 'success') {
                    this.setData(data.data)
                    if (data.data.dataList.length) {
                        let signlist = data.data.dataList
                        var arrg = []
                        for (let j = 0; j < signlist.length; j++) {
                            arrg.push(parseInt(signlist[j].signTime.substr(signlist[0].signTime.length - 2, signlist[0].signTime.length)))

                        }
                        console.log(arrg)
                        console.log(this.data.todayIndex)
                        console.log(arrg.indexOf(this.data.todayIndex))
                        if (signlist.length && arrg.indexOf(this.data.todayIndex) > -1) {
                            this.setData({
                                todaySign: true
                            })
                        }
                    }

                }
            })
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        common.request(api.signList, {}, 'post').then(data => {
            console.log(data)
            if (data.type == 'success') {
                this.setData({
                    signDays: data.data.signDays,
                    signTotal: data.data.signTotal
                })
                if (data.type == 'success') {
                    this.setData(data.data)
                    if (data.data.dataList.length) {
                        let signlist = data.data.dataList
                        var arrg = []
                        for (let j = 0; j < signlist.length; j++) {
                            arrg.push(parseInt(signlist[j].signTime.substr(signlist[0].signTime.length - 2, signlist[0].signTime.length)))

                        }
                        console.log(arrg)
                        console.log(this.data.todayIndex)
                        console.log(arrg.indexOf(this.data.todayIndex))
                        if (signlist.length && arrg.indexOf(this.data.todayIndex) > -1) {
                            this.setData({
                                todaySign: true
                            })
                        }
                    }

                }
            }
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})